package ldkxingzhe.l365calendar.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

import ldkxingzhe.l365calendar.model.ScheduleEntity;

/**
 * 日程部分的数据库操作
 * Created by ke on 16-2-14.
 */
public class ScheduleDao {
    private L365CalendarOpenHelper helper;

    public ScheduleDao(Context context){
        helper = L365CalendarOpenHelper.getInstance(context);
    }

    public long insert(@NonNull ScheduleEntity entity){
        return helper.getWritableDatabase()
                .insertOrThrow(L365CalendarOpenHelper.TABLE_SCHEDULE, null,
                        generateContentValue(entity));
    }

    private ContentValues generateContentValue(ScheduleEntity entity) {
        ContentValues resultValues = new ContentValues();
        resultValues.put(L365CalendarOpenHelper.CREATED, entity.getCreated());
        resultValues.put(L365CalendarOpenHelper.MODIFIED, entity.getModified());
        resultValues.put(L365CalendarOpenHelper.SCHEDULE_START_TIME, entity.getStartTime());
        resultValues.put(L365CalendarOpenHelper.SCHEDULE_DURATION, entity.getDuration());
        resultValues.put(L365CalendarOpenHelper.SCHEDULE_IS_ALL_DAY, entity.isAllDay());
        resultValues.put(L365CalendarOpenHelper.SCHEDULE_TITLE, entity.getTitle());
        resultValues.put(L365CalendarOpenHelper.SCHEDULE_LOCATION, entity.getLocation());
        resultValues.put(L365CalendarOpenHelper.SCHEDULE_DESCRIPTION, entity.getDescription());
        resultValues.put(L365CalendarOpenHelper.SCHEDULE_COMPLETED, entity.isCompleted());
        resultValues.put(L365CalendarOpenHelper.SCHEDULE_REPEAT_TYPE, entity.getRepeatType());
        resultValues.put(L365CalendarOpenHelper.SCHEDULE_REPEAT_STOP_TIME, entity.getRepeatStopTime());
        resultValues.put(L365CalendarOpenHelper.SCHEDULE_REPEAT_COUNT, entity.getRepeatCount());
        resultValues.put(L365CalendarOpenHelper.SCHEDULE_REPEAT_FREQUENCY, entity.getRepeatFrequency());
        resultValues.put(L365CalendarOpenHelper.SCHEDULE_IS_REPEAT_FINISHED, entity.isRepeatFinished());
        resultValues.put(L365CalendarOpenHelper.UUID, entity.getUuid());
        return resultValues;
    }

    public ScheduleEntity getScheduleEntity(@NonNull Cursor cursor){
        ScheduleEntity entity = new ScheduleEntity();
        entity.set_id(cursor.getInt(cursor.getColumnIndex(L365CalendarOpenHelper.ID)));
        entity.setCreated(cursor.getLong(cursor.getColumnIndex(L365CalendarOpenHelper.CREATED)));
        entity.setModified(cursor.getLong(cursor.getColumnIndex(L365CalendarOpenHelper.MODIFIED)));
        entity.setStartTime(cursor.getLong(cursor.getColumnIndex(L365CalendarOpenHelper.SCHEDULE_START_TIME)));
        entity.setDuration(cursor.getLong(cursor.getColumnIndex(L365CalendarOpenHelper.SCHEDULE_DURATION)));
        if(cursor.getInt(cursor.getColumnIndex(L365CalendarOpenHelper.SCHEDULE_IS_ALL_DAY)) == 0){
            entity.setIsAllDay(false);
        }else{
            entity.setIsAllDay(true);
        }
        entity.setTitle(cursor.getString(cursor.getColumnIndex(L365CalendarOpenHelper.SCHEDULE_TITLE)));
        entity.setLocation(cursor.getString(cursor.getColumnIndex(L365CalendarOpenHelper.SCHEDULE_LOCATION)));
        entity.setDescription(cursor.getString(cursor.getColumnIndex(L365CalendarOpenHelper.SCHEDULE_DESCRIPTION)));
        if(cursor.getInt(cursor.getColumnIndex(L365CalendarOpenHelper.SCHEDULE_COMPLETED)) == 0){
            entity.setCompleted(false);
        }else{
            entity.setCompleted(true);
        }
        entity.setRepeatType(cursor.getInt(cursor.getColumnIndex(L365CalendarOpenHelper.SCHEDULE_REPEAT_TYPE)));
        entity.setRepeatCount(cursor.getInt(cursor.getColumnIndex(L365CalendarOpenHelper.SCHEDULE_REPEAT_COUNT)));
        entity.setRepeatFrequency(cursor.getInt(cursor.getColumnIndex(L365CalendarOpenHelper.SCHEDULE_REPEAT_FREQUENCY)));
        if(cursor.getInt(cursor.getColumnIndex(L365CalendarOpenHelper.SCHEDULE_IS_REPEAT_FINISHED)) == 0){
            entity.setIsRepeatFinished(false);
        }else{
            entity.setIsRepeatFinished(true);
        }
        entity.setUuid(cursor.getString(cursor.getColumnIndex(L365CalendarOpenHelper.UUID)));
        return entity;
    }

    public Cursor selectAll(){
        return helper.getReadableDatabase()
                .rawQuery("SELECT * FROM " + L365CalendarOpenHelper.TABLE_SCHEDULE, null);
    }

    private Cursor queryByWhere(String[] projection, String where, String[] selectionArgs){
        return helper.getReadableDatabase()
                .query(L365CalendarOpenHelper.TABLE_SCHEDULE, projection, where, selectionArgs,
                        null, null, null);
    }

    public List<ScheduleEntity> getSomeDaySchedule(long millSeconds){
        List<ScheduleEntity> result = new ArrayList<>();
        String where = "DATE(" + L365CalendarOpenHelper.SCHEDULE_START_TIME  + "/1000 , \'unixepoch\')" +
                " = DATE(" + millSeconds / 1000 + ", \'unixepoch\')";
        Cursor cursor = queryByWhere(getAllProjection(), where, null);
        while (cursor.moveToNext()){
            result.add(getScheduleEntity(cursor));
        }
        cursor.close();
        return result;
    }

    private String[] projection;
    @NonNull
    private String[] getAllProjection(){
        if(projection == null){
            projection = new String[]{
                    L365CalendarOpenHelper.ID,L365CalendarOpenHelper.CREATED, L365CalendarOpenHelper.MODIFIED,
                    L365CalendarOpenHelper.SCHEDULE_COMPLETED, L365CalendarOpenHelper.SCHEDULE_IS_ALL_DAY, L365CalendarOpenHelper.SCHEDULE_DESCRIPTION,
                    L365CalendarOpenHelper.SCHEDULE_DURATION, L365CalendarOpenHelper.SCHEDULE_START_TIME, L365CalendarOpenHelper.SCHEDULE_IS_REPEAT_FINISHED,
                    L365CalendarOpenHelper.SCHEDULE_LOCATION, L365CalendarOpenHelper.SCHEDULE_REPEAT_FREQUENCY, L365CalendarOpenHelper.SCHEDULE_REPEAT_COUNT,
                    L365CalendarOpenHelper.SCHEDULE_REPEAT_STOP_TIME, L365CalendarOpenHelper.SCHEDULE_TITLE, L365CalendarOpenHelper.SCHEDULE_REPEAT_TYPE,
                    L365CalendarOpenHelper.UUID
            };
        }
        return projection;
    }
}
